﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo.Core;
using Apollo.Web;
using Apollo;

namespace Apollo.Website
{
    public partial class Controls_Edit_FormItem : System.Web.UI.UserControl, IFormItem
    {
        public FormItem Item { get; set; }

        public object TargetObject { get; set; }

        protected Control FormItemControl;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Item.CustomFormItemControl))
                LoadFormItemControl(this.Item.CustomFormItemControl);
            else if (this.Item.Projection.PropertyType == null 
                || this.Item.Projection.PropertyType == typeof(int)
                || this.Item.Projection.PropertyType == typeof(decimal)
                || this.Item.Projection.PropertyType == typeof(float)
                || this.Item.Projection.PropertyType == typeof(double)
                || this.Item.Projection.PropertyType == typeof(string))
                LoadFormItemControl("Default");
            else if (this.Item.Projection.PropertyType == typeof(bool))
                LoadFormItemControl("Bool");
            else if (this.Item.Projection.PropertyType == typeof(DateTime)
                || this.Item.Projection.PropertyType == typeof(DateTime?))
                LoadFormItemControl("DateTime");
            else if (this.Item.Projection.PropertyType.IsEnum)
                LoadFormItemControl("Enum");
            else if (this.Item.Projection.IsRelation
                && this.Item.Projection.EntityProperty.BelongToEntity != null
                && this.Item.Projection.EntityProperty.BelongToEntity.Loading == EntityLoading.LowLoading)
                LoadFormItemControl("RelationLowLoading");
            else if(this.Item.Projection.IsRelation)
                LoadFormItemControl("Relation");
            else
                LoadFormItemControl("Default");
        }

        protected void LoadFormItemControl(string fiType)
        {
            string pathToControl = "~/Controls/Edit/FormItem" + fiType + ".ascx";

            if(!System.IO.File.Exists(MapPath(pathToControl)))
                pathToControl = "~/Controls/Edit/FormItemDefault.ascx";

            try
            {
                this.FormItemControl = this.LoadControl(pathToControl);
                IFormItem fi = this.FormItemControl as IFormItem;
                fi.Item = this.Item;
                fi.TargetObject = this.TargetObject;

                PlaceHolderMain.Controls.Add(this.FormItemControl);
            }
            catch//(Exception ex)
            {
                //(this.Page.FindControl("MessageList") as MessageList).Add(new ActionMessage(ex.Message, ActionMessageType.Error));
            }
        }

        public ActionMessage Save(object obj)
        {
            return (this.FormItemControl as IFormItem).Save(obj);
        }
    }
}